/**
 * Copyright © 2018 organization baomidou
 * <pre>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * <pre/>
 */
package org.luxor.commons.websocket.component.processor;

/**
 * 决定 Endpoint URI
 *
 * @author Mr.Yan  @date: 2021/9/1
 */
public abstract class WsUriProcessor {

    private WsUriProcessor nextProcessor;

    public void setNextProcessor(WsUriProcessor dsProcessor) {
        this.nextProcessor = dsProcessor;
    }

    /**
     * 抽象匹配条件 匹配才会走当前执行器否则走下一级执行器
     *
     * @param key WebSocketClient注解里的内容
     * @return 是否匹配
     */
    public abstract boolean matches(String key);

    /**
     * 决定Endpoint URI
     * <pre>
     *     调用底层doDetermine，
     *     如果返回的是null则继续执行下一个，否则直接返回
     * </pre>
     *
     * @param key WebSocketClient注解里的内容
     * @return 数据源名称
     */
    public String determine(String key) {
        if (matches(key)) {
            String EndpointUrl = doDetermine(key);
            if (EndpointUrl == null && nextProcessor != null) {
                return nextProcessor.determine(key);
            }
            return EndpointUrl;
        }
        if (nextProcessor != null) {
            return nextProcessor.determine(key);
        }
        return key;
    }

    /**
     * 抽象最终决定数据源
     *
     * @param key WebSocketClient注解里的内容
     * @return 数据源名称
     */
    public abstract String doDetermine(String key);
}
